#!/usr/bin/ruby

# **************************************************************************
# **************************************************************************
# **                                                                      **
# **  (c) 2000-2011 SOFHA GmbH                                            **
# **  This product is copyright protected by international laws.          **
# **  Any attempt to reverse engineer or copy this code is considered to  **
# **  be a copyright infringement and will entail legal steps by SOFHA.   **
# **                                                                      **
# **************************************************************************
# **************************************************************************

MANUAL_VERSION = "v20"
MANUAL_BASENAME = "1200_prt_sf_ug"

def moveManual(lang)
    filename = "Manuals/#{MANUAL_BASENAME}_#{lang}_#{MANUAL_VERSION}.pdf"
    if !File.exists?(filename)
        filename = "Manuals/#{MANUAL_BASENAME}_eng_#{MANUAL_VERSION}.pdf"
    end
    system "chown \"#{ENV['USER']}\":`id -g -nr \"#{ENV['USER']}\"` \"#{filename}\""
    system "chmod 644 \"#{filename}\""
    system "cp -p \"#{filename}\" ~/Desktop"
end

key_re = /^\s*<key>([A-Za-z]+[_A-Za-z0-9]*)<\/key>\s*$/
value_re = /^\s*<string>([-_:.\/0-9A-Za-z]+)<\/string>\s*$/
appleLanguage = "en";

preffilename = "#{ENV['INSTALLER_TEMP']}/de.sofha.global.plist"
system "plutil -convert xml1 -o \"#{preffilename}\" ~/Library/Preferences/.GlobalPreferences.plist"

File.open(preffilename,"r") do |plist|
    currentKey = ""
    plist.each_line do |next_line|
    if key_re =~ next_line
        currentKey = $1
        currentValue = ""
    elsif value_re =~ next_line
        currentValue = $1
        if currentKey == "AppleLanguages"
            if currentValue == "en" || currentValue == "English" || currentValue == "en-US" || currentValue == "en-GB"
                moveManual("eng")
                break
            elsif currentValue == "fr" || currentValue == "French" || currentValue == "fr-CA" || currentValue == "fr-CH"
                moveManual("fra")
                break
            elsif currentValue == "it" || currentValue == "Italian"
                moveManual("ita")
                break
            elsif currentValue == "de" || currentValue == "German" || currentValue == "de-CH" || currentValue == "de-AT"
                moveManual("deu")
                break
            elsif currentValue == "es" || currentValue == "Spanish" || currentValue == "es_ES" || currentValue == "es-419"
                moveManual("esp")
                break
            elsif currentValue == "ko" || currentValue == "Korean"
                moveManual("kor")
                break
            elsif currentValue == "ja" || currentValue == "Japanese"
                moveManual("jpn")
                break
            elsif currentValue == "zh_CN" || currentValue == "zh-Hans"
                moveManual("chn")
                break
            elsif currentValue == "zh_TW" || currentValue == "zh-Hant"
                moveManual("ctw")
                break
            else
                moveManual("eng")
                break
            end
            currentValue = ""
        end
      end
    end
end
